;;;;;;;;;;
;; Support for compilation: If the buggy file will not be found by next-error,
;; customize compilation-search-path.
(defvar my-compiler-history nil
  "The history of the last my-desktop-compiler function calls.")

(defvar my-default-dir-list nil
  "List stores the `default-directory' of buffers, organized ad
  apush/pop stack.")

(defvar my-build-tags-history nil
  "The history of the last `my-desktop-build-tags' function calls.")

(defvar my-desktop-grep-history nil
  "The history of the last `my-desktop-grep' function calls.")

(defun my-push-to-desktop-dir ()
  (push default-directory my-default-dir-list)
  (cd desktop-dirname))

(defun my-pop-default-dir ()
  (cd (pop my-default-dir-list)))

(defun my-desktop-compiler (cmd)
  "Compile my actual project."
  (interactive
   (progn
     (list (read-from-minibuffer "Compiler expression: "
                                 (car my-compiler-history)
                                 nil nil
                                 'my-compiler-history))))
  (my-push-to-desktop-dir)
  (compile cmd)
  (my-pop-default-dir))



(defun my-desktop-build-tags (cmd)
  "Build tags file for my actual project."
  (interactive
   (progn
     (list (read-from-minibuffer "Build tags expression: "
                                 (car my-build-tags-history)
                                 nil nil
                                 'my-build-tags-history))))
  (tags-reset-tags-tables)
  (my-push-to-desktop-dir)
  (shell-command cmd)
  (my-pop-default-dir)
  (visit-tags-table-buffer (concat desktop-dirname)))


(defun my-desktop-dirname ()
  "A simple function to display the directory of the actual used
desktop."
  (interactive)
  (if  (stringp desktop-dirname)
      (message "Desktop located in %s." desktop-dirname)))


(defun my-desktop-grep (command-args)
  "This function supports grep functionality supported by other
tools then the native grep command. Some VCS have an integraed grep function
for files under VC. This function can access the VC grep feature. Actually,
ID, Bazaar and Mercurial are supported. "
  (interactive
   (let* ((desktop-grep-command
           (cond ((file-accessible-directory-p (concat desktop-dirname "/.bzr"))
                  (concat vc-bzr-program " grep -n " (thing-at-point 'symbol)))
                 ((file-accessible-directory-p (concat desktop-dirname "/.hg"))
                  (concat "hg grep -n " (thing-at-point 'symbol)))
                 ((file-exists-p (concat desktop-dirname "/ID"))
                  (concat "lid -R grep " (thing-at-point 'symbol)))
                 (t
                  grep-command))))
     (if desktop-grep-command
         (list (read-shell-command "Run grep: " desktop-grep-command 'my-desktop-grep-history))
       ;; No default was set
       (read-string "No advanced or standard grep is not supported. Press RET.")
       (list nil))))
  (when command-args
    (let ((null-device nil))		; see grep
      (my-push-to-desktop-dir)
      (grep command-args)
      (my-pop-default-dir))))


(defun my-desktop-simplify-dirs-in-buffer ()
  "Function has the intention to strip the `desktop-dirname' from
the compilation or grep buffer. This would save horizontal screen
space and the important buffer content can be read more
comfortable."
  (interactive)
  (setq buffer-read-only nil)
  ;; Windows: Make standard path seperators (backslashes to slahes)
  (beginning-of-buffer)
  (save-match-data (while (re-search-forward "////" nil t)
                     (replace-match "/")))
  (goto-line 2)
  (save-match-data (while (re-search-forward (regexp-quote desktop-dirname) nil t)
                     (replace-match "./")))
  (goto-line 1))

(add-hook 'compilation-mode-hook
          (lambda ()
            (local-set-key (kbd "s") 'my-desktop-simplify-dirs-in-buffer)))

(provide 'resource/desktop)
